﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using Common;

namespace WorkDAL.Meterials
{
    public class MaterialDataAccess
    {
        ListDataContext listData = new ListDataContext();

        public static MaterialDataAccess Instance()
        {
            return new MaterialDataAccess();
        }

        public List<LiMaterial> Material_GetList(string materialId)
        {
            List<LiMaterial> materialList = new List<LiMaterial>();

            using (SqlConnection sqlConnection = new SqlConnection(CommonSettings.ConnectionString))
            {
                sqlConnection.Open();
                SqlCommand sqlCommand = new SqlCommand("Metarial_GetList", sqlConnection);
                sqlCommand.CommandType = CommandType.StoredProcedure;
                sqlCommand.Parameters.Add("@MaterialId", SqlDbType.VarChar).Value = materialId;

                using (SqlDataReader sqlDataReader = sqlCommand.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    while (sqlDataReader.Read())
                    {
                        if (sqlDataReader.HasRows)
                        {
                            LiMaterial item = new LiMaterial();

                            if (sqlDataReader["MaterialGroupId"] != DBNull.Value)
                            {
                                item.MaterialGroupId = sqlDataReader["MaterialGroupId"].ToString();
                            }

                            if (sqlDataReader["MaterialId"] != DBNull.Value)
                            {
                                item.MaterialId = sqlDataReader["MaterialId"].ToString();
                            }

                            if (sqlDataReader["MaterialName"] != DBNull.Value)
                            {
                                item.MaterialName = sqlDataReader["MaterialName"].ToString();
                            }

                            if (sqlDataReader["Unit"] != DBNull.Value)
                            {
                                item.Unit = sqlDataReader["Unit"].ToString();
                            }

                            materialList.Add(item);
                        }
                    }
                }
            }

            return materialList;
        }

        public LiMaterial LiMaterial_GetById(string limaterialId)
        {
            var liMaterial = from c in listData.LiMaterials
                             where c.MaterialId == limaterialId
                             select c;

            return (LiMaterial)(liMaterial.ToList()[0]);
        }

        public void CreateMaterial(LiMaterial item)
        {
            listData.LiMaterials.InsertOnSubmit(item);
            listData.SubmitChanges();
        }

        public void UpdateMaterial(LiMaterial item)
        {
            LiMaterial liMaterial = listData.LiMaterials.Single(p => p.MaterialId == item.MaterialId);

            liMaterial.MaterialGroupId = item.MaterialGroupId;
            liMaterial.MaterialName = item.MaterialName;
            liMaterial.Unit = item.Unit;
            liMaterial.UnitChange1 = item.UnitChange1;
            liMaterial.UnitChange2 = item.UnitChange2;
            liMaterial.UnitChangeRate1 = item.UnitChangeRate1;
            liMaterial.UnitChangeRate2 = item.UnitChangeRate2;
            liMaterial.BuyPrice = item.BuyPrice;
            liMaterial.SellPrice = item.SellPrice;

            listData.SubmitChanges();
        }



    }
}
